package com.example.comiccenter.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.comiccenter.dao.CollectMapper;
import com.example.comiccenter.dao.ComicMapper;
import com.example.comiccenter.dao.LikeMapper;
import com.example.comiccenter.domain.Collect;
import com.example.comiccenter.domain.Comic;
import com.example.comiccenter.domain.Likes;
import com.example.comiccenter.utils.ResultData;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;

@RestController
@RequestMapping("/joint")
public class JointController {

    @Resource
    private ComicMapper comicMapper;
    @Resource
    private LikeMapper likeMapper;
    @Resource
    private CollectMapper collectMapper;

    @PostMapping("/getComicInfo")
    public ResultData getComicInfo(@RequestParam String username,@RequestParam Integer comicId){
        System.out.println(comicId);
        //查询comic信息以及用户的点赞收藏信息
        Comic comic = comicMapper.selectById(comicId);
        QueryWrapper<Collect> collectQueryWrapper = new QueryWrapper<Collect>();
        collectQueryWrapper.eq("username",username).eq("comicId",comicId);
        Boolean isCollect = collectMapper.selectCount(collectQueryWrapper) > 0;
        QueryWrapper<Likes> likeQueryWrapper = new QueryWrapper<Likes>();
        likeQueryWrapper.eq("username",username).eq("comicId",comicId);
        Boolean isLike = likeMapper.selectCount(likeQueryWrapper) > 0;

        HashMap<String, Object> data = new HashMap<>();
        data.put("comicId",comic.getId());
        data.put("comicName",comic.getName());
        data.put("area",comic.getArea());
        data.put("score",comic.getScore());
        data.put("author",comic.getAuthor());
        data.put("type",comic.getType());
        data.put("isEnd",comic.getIsEnd());
        data.put("introduce",comic.getIntroduction());
        data.put("cover",comic.getCover());
        data.put("isCollect",isCollect);
        data.put("isLike",isLike);
        return ResultData.success("查询成功",data);
    }
}
